<?

define("_1ST"                  , 0);
define("_2ND"                  , 1);
define("_DEFAULT_DECREMENT"    , 1);

/**
* To Cancel this tx there are several steps
* to perform :
* 1. Check if model is _PEMBELIAN
* 2. Decrease the inventory balance
* 
*/

/**
* Check if model is _PEMBELIAN
* in modelHistory DB
*
*/

/**
* We need to know where is the warehouse at
*
*/
fetchWarehouse($COracle,
               $thisJobId,
               $srcDbHdr,
               $warehouse);
               
if(!$warehouse){
  
  usrDie("Error: Data corrupted, Warehouse not found");
}

$dstDbInventory = "WHS_" . $warehouse;

fetchNDetail($COracle,
             $thisJobId,
             $srcDbDtl,
             $nDetail,
             $noDetail,
             $lstDetail);
             
for($iDtl = 0; $iDtl < $nDetail; $iDtl++){
  
  /**
  * thisJobId will no longer takes 
  * apprThisJobId since is current 
  * selected Tx's header job id 
  * 
  * recvId = $lstDetail["RECV_REF"] 
  * reflects the source of this tx 
  * thus we could process the JobId of details
  * to manage the history and inventory
  *
  */
  $recvId = $lstDetail["RECV_REF"][$iDtl];
  
  fetchNSubDetail($COracle,
                  $thisJobId,
                  $noDetail[$iDtl],
                  $srcDbSubDtl,
                  $nSub,
                  $subDetail);
  
  for($iSub = 0; $iSub < $nSub; $iSub++){
    
    /**
    * Extract the serial number
    * if serial number is exists
    * YES: Process model history entry
    * NO : process model without serNumber
    *
    */
    $exSerNumber = explode(_DEFAULT_SEPARATOR_02, 
                           $subDetail["SER_NUMBER"][$iSub]);
    
    if($exSerNumber[_1ST] != _MANUAL_KEY_IN){
    
      $serNumber = explode("-", 
                           $subDetail["SER_NUMBER"][$iSub]);
      /**
      * Fetch the head and tail
      *
      */
      procSerNumRealValue($subDetail["HEAD"][$iSub],
                          $subDetail["TAIL"][$iSub],
                          $serNumber[_2ND],
                          $serNumber[_1ST],
                          $theRange);
      dbgInfo("theRange", $theRange);
      
      for($iSer = 0 ; $iSer <= $theRange; $iSer++){
        
        procSerNumber($subDetail["HEAD"][$iSub],
                      $subDetail["TAIL"][$iSub],
                      $serNumber[_1ST],
                      $iSer,
                      $theSerial);
      
        delModelEntry($COracle,
                      $recvId,
                      $noDetail[$iDtl],
                      $subDetail["MODEL"][$iSub],
                      $subDetail["MODEL_ID"][$iSub],
                      $subDetail["UNIT"][$iSub],
                      $theSerial,
                      $srcDbHistory);
        
        decrInventory($COracle,
                      $subDetail["MODEL"][$iSub],
                      $subDetail["MODEL_ID"][$iSub],
                      $subDetail["UNIT"][$iSub],
                      _DEFAULT_DECREMENT,
                      $dstDbInventory);
      }
    }else{
      
      $theRange = $subDetail["RECV_QTY"][$iSub];
      
      /** 
      * We need to check if the range is equal 
      * to manual key in entry
      *
      */
      $manInJobId = $exSerNumber[_2ND];
      
      fetchManInDtl($COracle,
                    $manInJobId,
                    $srcDbManInDtl,
                    $nManInput,
                    $resManInput);
      
      if($nManInput == $theRange){
      
        for($iSer = 0; $iSer < $theRange; $iSer++){
          
          $serNumber = $resManInput["SER_NUMBER"][$iSer];
          
          delModelEntry($COracle,
                        $recvId,
                        $noDetail[$iDtl],
                        $subDetail["MODEL"][$iSub],
                        $subDetail["MODEL_ID"][$iSub],
                        $subDetail["UNIT"][$iSub],
                        $serNumber,
                        $srcDbHistory);
          
          decrInventory($COracle,
                        $subDetail["MODEL"][$iSub],
                        $subDetail["MODEL_ID"][$iSub],
                        $subDetail["UNIT"][$iSub],
                        _DEFAULT_DECREMENT,
                        $dstDbInventory);
        }
      }else{
        
        usrDieJs("Error: Manual Input SER_NUMBER is not equal to RECV_QTY");
      }
    }
    
  }
}

?>